package com.abo.yaas.webservice.utils;

import com.abo.yaas.domain.Auction;
import com.abo.yaas.webservice.CriteriaQueryUtil;
import com.abo.yaas.webservice.constants.Operation;
import com.abo.yaas.webservice.exceptions.WebServiceException;
import com.abo.yaas.webservice.hibernate.WSFilter;

public class AuctionFacade {

    public static Auction postNewAuction(final Auction auction) throws WebServiceException {
        final String title = auction.getTitle();
        final WSFilter<Auction> filter = new WSFilter<Auction>("title", title, Operation.EQ, Auction.class);
        final Object id = (CriteriaQueryUtil.getLastEntityId(filter, null, Auction.class)).getValue();
        if (id != null) {
            throw new WebServiceException("The auction with such a title exists already!");
        }
        final Auction result = CriteriaQueryUtil.saveEntity(auction); 
        return result;
    }
}
